import React, { useState, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux'

import { Table, Flex } from 'antd';
import { GROUP_DELETED, GROUP_FIRST_ROUND, GROUP_HAS_TARGET } from '../../contants.js'
import useTableColumns from './useTableColumns'
import { fetchJobList, fetchJobDetail } from '../../redux/actions.js'
import { putSelectedRows, rebuildDataListFiltered } from '../../redux/reducers.js';

function SmartTable(props) {
  let page = 1
  let pageSize = 50
  const dispatch = useDispatch()
  const selectedRowKeys = useSelector(state => state.job.selectedRowKeys)
  const filters = useSelector(state => state.job.filters)

  useEffect(()=>{
    dispatch(fetchJobList())
  }, [])

  useEffect(()=>{
    dispatch(rebuildDataListFiltered())
  }, [filters])

    // const onHardDelete = ({ hard = false })=>{
    //   const { selectedRowKeys } = this.state;
    //   window.electronAPI.deleteJobList({ids: selectedRowKeys, hard}, (response) => {
    //     const { data } = response
    //     this.setState({
    //       dataBackend: this.transData(data)
    //     })

    //     console.log('deleteJobList')
    //   })
    // }

    // ====== render ======
    const getCurrentPageKeys = () => {
      return dataShow.filter((value, index) => {
        return index + 1 > (page - 1) * pageSize && index + 1 <= page * pageSize
      })
      .map(item => item['lid'])
    }

    const getSelectedRows = () => {
      return selectedRowKeys
    }

    const setSelectedRows = (value) => {
      dispatch(putSelectedRows(value))
    }

    const onClickTitle = (lid)=> {
      // console.log('onClickTitle', lid)
      dispatch(fetchJobDetail(lid))
    }

    const columns = useTableColumns({
      getCurrentPageKeys,
      getSelectedRows,
      setSelectedRows,
      onClickTitle
    })

    const currentGroup = useSelector(state => state.job.group)
    const dataShow = useSelector(state => state.job.dataListFiltered.filter(item => {
      // console.log('+++', item['group'])
      // return item['group'] == currentGroup
      return item['group'] != GROUP_DELETED
      
    }))
    return (
      <Table
        columns={columns}
        dataSource={dataShow}
        size="small"
        scroll={{ y:39*15}}
        pagination={{ pageSize, total: dataShow.length,
          showTotal: (total, range) => { return `第${range[0]}-${range[1]}条  共${total} 条`},
          onChange: (page, pageSize) => {
            page = page
            pageSize = pageSize
            setSelectedRows([])
          }
        }}
      />)
}

export default SmartTable
